﻿import flash.display.*;
import flash.geom.ColorTransform;
import flash.geom.Transform;

class com.dinmax.flash.draw.utilities.ColorSelector
extends MovieClip {

	public static var IMG_URL:String = "colorwheel.png";
	
	private var root:MovieClip;
	private var _value:Number;
	private var mcImage:MovieClip;
	private var bmpData:BitmapData;
	private var listener:Object;

	function ColorSelector(root:MovieClip){
		root ? this.root = root : this.root = this;
		init();
		return;
	};
	
	private function init() {
		//trace("ColorSelector init");
		var r:ColorSelector = this;
		var bmpData:BitmapData;
		this.mcImage = root.createEmptyMovieClip("mcImage", root.getNextHighestDepth());
		this.mcImage.onPress = function(){
			r.setValue(bmpData.getPixel(_xmouse, _ymouse));
			this.attachMovie("cross", "mcCross", 2, { _x: this._xmouse, _y: this._ymouse} );
			return;
		}
		
		var original:MovieClip = mcImage.createEmptyMovieClip("original", 1);
		var cargando:Object = new Object();
		cargando.onLoadInit = function() {
			var w:Number = original._width;
			var h:Number = original._height;
			bmpData = new BitmapData(w, h);
			bmpData.draw(original);
			original.attachBitmap(bmpData, 2, "auto", true);
		};
		var imagen_mcl:MovieClipLoader = new MovieClipLoader();
		imagen_mcl.addListener(cargando);
		imagen_mcl.loadClip(ColorSelector.IMG_URL, original);
	};

	public function getValue(){
		//trace("ColorSelector getValue= "+this._value);
		return this._value;
	};

	public function setValue(nColor:Number){
		//trace("ColorSelector setValue= "+nColor);
		mcImage.mcCross._visible = false;
		this._value = nColor;
		var colorTrans:ColorTransform = new ColorTransform();
		colorTrans.rgb = nColor;
		var trans:Transform = new Transform(root.mcRect);
		trans.colorTransform = colorTrans;
		
		listener.colorChange(this._value);
		return;
	};
	
}